﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using OfficeBusiness.Model;
using System.Windows;
using System.Windows.Input;
using System.Collections;

namespace OfficeBusiness.ViewModel
{
    public class DepartmentViewModel : ViewModelBase
    {
        private IBusinessModel businessModel;
        private ObservableCollection<Department> departmentsData;
        private Department selectedItem;
        private ICommand createCommand;
        private ICommand refreshCommand;
        private ICommand deleteRowCommand;
        private ICommand updateCommand;

        public DepartmentViewModel(IBusinessModel businessModel)
        {
            this.businessModel = businessModel;
            this.GetDepartmentDetails();
            this.businessModel.DepartmentSelectedEvent += new EventHandler(DepartmentSelected);
        }

        private void GetDepartmentDetails()
        {
            var depts = this.businessModel.GetDepartmentsDetails();
            foreach (Department dept in depts.ToList())
            {
                this.DepartmentsData.Add(dept);
            }
        }

        private void DepartmentSelected(object departmentVisibility, EventArgs e)
        {
            this.DepartmentsData.Clear();
            this.GetDepartmentDetails();
        }

        public ObservableCollection<Department> DepartmentsData
        {
            get
            {
                if (this.departmentsData == null)
                {
                    this.departmentsData = new ObservableCollection<Department>();
                }

                return this.departmentsData;
            }
        }

        public Department SelectedItem
        {
            get
            {
                return this.selectedItem;
            }

            set
            {
                this.selectedItem = value;
                OnPropertyChanged("SelectedItem");
            }
        }

        public ICommand CreateCommand
        {
            get
            {
                if (this.createCommand == null)
                {
                    this.createCommand = new RelayCommand(param => this.CreateRow(), param => true);
                }

                return this.createCommand;
            }
        }

        public ICommand RefreshCommand
        {
            get
            {
                if (this.refreshCommand == null)
                {
                    this.refreshCommand = new RelayCommand(param => this.RefreshTable(), param => true);
                }

                return this.refreshCommand;
            }
        }

        public ICommand DeleteRowCommand
        {
            get
            {
                if (this.deleteRowCommand == null)
                {
                    this.deleteRowCommand = new RelayCommand(param => this.DeleteRow(), param => true);
                }

                return this.deleteRowCommand;
            }
        }

        public ICommand UpdateCommand
        {
            get
            {
                if (this.updateCommand == null)
                {
                    this.updateCommand = new RelayCommand(param => this.Update(), param => true);
                }

                return this.updateCommand;
            }
        }

        private void Update()
        {
            var deptList = from e in this.DepartmentsData select e;
            bool updateStatus = this.businessModel.UpdateRows<ViewModel.Department>(deptList.ToList());
            if (!updateStatus)
            {
                MessageBox.Show("update falied");
                this.RefreshTable();
            }
        }

        private void DeleteRow()
        {
            if (this.selectedItem != null)
            {
                bool deleteStatus = this.businessModel.DeleteSelectedRow<ViewModel.Department>(this.SelectedItem);
                if (!deleteStatus)
                {
                    MessageBox.Show("Delete failed");
                }
                else
                {
                    this.DepartmentsData.Remove(this.SelectedItem);
                }
                
            }
            else
            {
                MessageBox.Show("Please select a row");
            }
        }

        private void CreateRow()
        {
            var deptsList = this.businessModel.GetDepartmentsDetails();
            var newItems = from itms in this.DepartmentsData where !(from exItems in deptsList select exItems.DepartmentNumber).Contains(itms.DepartmentNumber) select itms;
            bool createStatus = this.businessModel.InsetRows<ViewModel.Department>(newItems.ToList());
            if (!createStatus)
            {
                MessageBox.Show("Create failed");
                this.RefreshTable();
            }
        }

        private void RefreshTable()
        {
            this.DepartmentsData.Clear();
            this.GetDepartmentDetails();
        }
    }
}
